package com.tuanzi.loan.business.repository.family.impl;

import com.tuanzi.loan.business.repository.family.custom.LoanLargeNewDeliveryRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.family.largeamount.LargeNewDeliveryRequest;
import com.tuanzi.loan.vo.response.family.largeamount.LargeNewDeliveryResponse;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import static com.tuanzi.loan.generated.jooq.loan.Tables.LOAN_LARGE_NEW_DELIVERY;
import static com.tuanzi.loan.generated.jooq.loan.Tables.LOAN_WHOLESALE_COMMENT;

/**
 * User: liangZongXi
 * Create time: 11:24 2019/7/9
 * Description:
 */
public class LoanLargeNewDeliveryRepositoryImpl extends JOOQBaseRepository implements LoanLargeNewDeliveryRepositoryCustom {

    @Override
    public Page<LargeNewDeliveryResponse> list(LargeNewDeliveryRequest searchRequest, Pageable pageable) {
        fields(LOAN_LARGE_NEW_DELIVERY.ID, LOAN_LARGE_NEW_DELIVERY.CITY_STR, LOAN_LARGE_NEW_DELIVERY.SORT,
                LOAN_WHOLESALE_COMMENT.OWNERNAME);

        leftJoin(LOAN_WHOLESALE_COMMENT, LOAN_LARGE_NEW_DELIVERY.LARGE_TYPE.eq(LOAN_WHOLESALE_COMMENT.TYPE));

        contains(LOAN_LARGE_NEW_DELIVERY.CITY_STR, searchRequest.getCity());
        eq(LOAN_LARGE_NEW_DELIVERY.LARGE_TYPE, searchRequest.getType());
        eq(LOAN_LARGE_NEW_DELIVERY.SORT, searchRequest.getSort());

        return search(pageable, record -> {
            LargeNewDeliveryResponse response = new LargeNewDeliveryResponse();
            response.setId(record.get(LOAN_LARGE_NEW_DELIVERY.ID));
            response.setCityStr(record.get(LOAN_LARGE_NEW_DELIVERY.CITY_STR));
            response.setLargeTypeName(record.get(LOAN_WHOLESALE_COMMENT.OWNERNAME));
            response.setSort(record.get(LOAN_LARGE_NEW_DELIVERY.SORT));

            return response;
        });
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_LARGE_NEW_DELIVERY;
    }
}
